package com.betterjr.modules.risk.loan.dubbo;

import java.lang.reflect.InvocationTargetException;
import java.util.Map;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.BeanUtilsBean;
import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.dubbo.config.annotation.Service;
import com.betterjr.common.mapper.BeanMapper;
import com.betterjr.common.web.AjaxObject;
import com.betterjr.modules.risk.loan.ILoanProjectRelationService;
import com.betterjr.modules.risk.loan.entity.LoanProjectRelation;
import com.betterjr.modules.risk.loan.service.LoanProjectRelationService;
import com.betterjr.modules.risk.loan.util.BetterBeanUtils;

@Service(interfaceClass = ILoanProjectRelationService.class)
public class LoanProjectRelationDubboService implements ILoanProjectRelationService {

    @Autowired
    private LoanProjectRelationService relationService;

    @Override
    public String webCreateRelationByRefNo(String anRefNos) {
        return AjaxObject.newOk("创建贷款项目成功", relationService.createRelationByRefNo(anRefNos)).toJson();
    }

    @Override
    public String webFindLoanProjectReltionDetailById(Long anLoanId) {

        return AjaxObject.newOk("查询贷款项目成功", relationService.findLoanProjectReltionDetailById(anLoanId)).toJson();
    }

    @Override
    public String webSaveDeletedLoanById(Long anLoanId) {

        return AjaxObject.newOk("废止贷款项目成功", relationService.saveDeletedLoanById(anLoanId)).toJson();
    }

    @Override
    public String webQueryLoanRelations(Map<String, Object> anQueryMap, String orderData, String anFlag, int anPageNum,
            int anPageSize) {

        return AjaxObject
                .newOkWithPage("贷款项目查询成功",
                        relationService.queryLoanRelations(anQueryMap, orderData, anFlag, anPageNum, anPageSize))
                .toJson();
    }

    @Override
    public String webSaveEffectiveOrStorageLoan(Map<String, Object> anQueryMap, String anOperatorType,
            String anAgreementIds) {
        LoanProjectRelation relation = new LoanProjectRelation();
        // BeanMapper.copy(anQueryMap, relation);
        // BetterBeanUtils.copyProperties2Bean(relation, anQueryMap);
        try {
            BeanUtils.copyProperties(relation, anQueryMap);
        }
        catch (Exception e) {
            e.printStackTrace();
            // BeanMapper.copy(anQueryMap, relation);
            BetterBeanUtils.copyProperties2Bean(relation, anQueryMap);
        }

        return AjaxObject
                .newOk("新增贷款项目成功", relationService.saveEffectiveOrStorageLoan(relation, anOperatorType, anAgreementIds))
                .toJson();
    }

}
